<script setup lang="ts">
import { loginAPI, loginByCodeAPI, sendCodeAPI } from '@/services/user'
import { useUserStore } from '@/stores'
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { showSuccessToast, showToast } from 'vant'
import { onUnmounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const mobile = ref('13230000001')
const password = ref('abc12345')
const isPass = ref(true)
const changePass = () => {
  isPass.value = !isPass.value
}

const code = ref('')

const second = ref(0)

let timerId: number
const onSendCode = async () => {
  const res = await sendCodeAPI(mobile.value)
  code.value = res.data.code
  second.value = 60
  timerId = setInterval(() => {
    second.value--
    if (second.value === 0) clearInterval(timerId)
  }, 100)
}

onUnmounted(() => {
  clearInterval(timerId)
})

const isShow = ref(true)
const agree = ref(false)

const router = useRouter()
const route = useRoute()

console.log('route  ----->  ', route.query)
const store = useUserStore()

const onSubmit = async () => {
  if (!agree.value) {
    showToast('请勾选同意登录协议')
    return
  }
  const res = isPass.value
    ? await loginAPI(mobile.value, password.value)
    : await loginByCodeAPI(mobile.value, code.value)
  store.saveUser(res.data)
  showSuccessToast('登录成功')

  const returnUrl = route.query.returnUrl
  if (returnUrl) {
    router.push(returnUrl as string)
  } else {
    router.push('/')
  }
}
</script>

<template>
  <div class="login-page">
    <cp-nav-bar right-text="注册"></cp-nav-bar>
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a @click="changePass" href="javascript:;">
        <span>{{ !isPass ? '密码登录' : '短信验证码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <van-form autocomplete="off" @submit="onSubmit">
      <van-field v-model="mobile" placeholder="请输入手机号" type="tel" :rules="mobileRules" />
      <van-field
        v-if="isPass"
        v-model="password"
        placeholder="请输入密码"
        :type="isShow ? 'password' : 'text'"
        :rules="passwordRules"
      >
        <template #button>
          <cp-icon @click="isShow = !isShow" :name="isShow ? 'login-eye-off' : 'login-eye-on'" />
        </template>
      </van-field>
      <van-field :rules="codeRules" v-model="code" v-if="!isPass" placeholder="请输入验证码">
        <template #button>
          <span class="send-code" v-show="second === 0" @click="onSendCode">发送验证码</span>
          <span v-show="second > 0">{{ second }}秒后获取</span>
        </template>
      </van-field>

      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit">登 录</van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>

    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <a
        href="https://graph.qq.com/oauth2.0/authorize?client_id=102015968&response_type=token&scope=all&redirect_uri=http%3A%2F%2Fconsult-patients.itheima.net%2Flogin%2Fcallback"
        class="icon"
      >
        <img src="@/assets/qq.svg" alt="" />
      </a>
    </div>
  </div>

  <!-- <div id="qq"></div> -->
</template>

<style lang="scss" scoped>
.login {
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}

.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .send-code {
    --cp-primary: #16c2a3;
    color: var(--cp-primary);
  }
}
</style>
